package com.peng.controller;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ImageContent;
import dev.langchain4j.data.message.TextContent;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.output.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Base64;

/**
 * @author love_ovo
 * @ClassName ChatLanguageModelController.java
 * @createTime 2025年05月13日 10:28:00
 */
@RestController
@Slf4j
public class ChatLanguageModelController {

    @Autowired
    private ChatLanguageModel chatLanguageModel;

    @Value("classpath:static/images/1.png")
    private Resource resource;

    //    http://localhost:9004/image/call
    @GetMapping(value = "/image/call")
    public String readImageContent() throws IOException {

        byte[] byteArray = resource.getContentAsByteArray();
        String base64Data = Base64.getEncoder().encodeToString(byteArray);

        System.out.println("**********base64Data： "+base64Data);

        //prompt 提示词添加
        UserMessage userMessage = UserMessage.from(
                TextContent.from("帮我分析这张图是一个什么行业的趋势图，并解释其表示的状态"),
                ImageContent.from(base64Data,"image/png")
        );

        Response<AiMessage> response = chatLanguageModel.generate(userMessage);

        System.out.println(response.content().text());

        return response.content().text();
    }
}